﻿Imports System.Text.RegularExpressions

Public Class CCANCELLabelState2 : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CCANCELLabelState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "CANCEL_LABEL") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask2 = DirectCast(task, CPDATask2)
        Debug.WriteLine("CCANCELLabelState.DoExecute")
        Dim checkInfo As CCheck = CCheck.GetIns
        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance

        Debug.WriteLine("CCANCELLabelState.DoExecute...START")

        Try

            Dim hasuFlg As Boolean = False
            ''バーコード判別
            Select Case CBarcode.GetBarcodeType(task.ProcessText)

                Case EnumBarcodeType.INNER_LABEL_CODE
                    'Unique.csvを検査
                    'なければ自分のユニークを検査->あれば自分のユニークDataで削除
                    'なければエラー
                    Dim uniqueCd As String = CBarcode.getBarcodeVl(task.ProcessText, EnumBarcodeType.INNER_LABEL_CODE, EnumBarcodeType.UNIQUE_CD)
                    Dim UniqueInfo As CCheck.UniqueCdData = checkInfo.ChkUniqueCdData(uniqueCd)
                    Dim inBoxQty As String = CCommon.Change36(CBarcode.getBarcodeVl(task.ProcessText, CBarcode.GetBarcodeType(task.ProcessText), _
                                                                   EnumBarcodeType.INBOX_QTY))

                    Dim sysitemCd As String = CBarcode.getBarcodeVl(task.ProcessText, CBarcode.GetBarcodeType(task.ProcessText), _
                                                                        EnumBarcodeType.SYSITEM_CD)
                    If "**" = inBoxQty Then
                        hasuFlg = True
                    Else
                        '10.06.09.LEE START ============>>
                        'バーコードーの数量部分に文字化けがあったらエラー
                        If Not Regex.IsMatch(inBoxQty, "[0-9A-Z]{2}") Then
                            '文字化けでもう一同読み込んでください。というエラー
                            CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                                  Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_031, Color.White)
                            task.ProcessText = ""
                            task.ActionCd = "LABEL"
                            nextState = GetNextState(task)
                            'CFileLog.WriteLog("CINNERLabelState.DoExecute END---", CFileLog.LEVEL_NORMAL, CFileLog.EnumLogType.READ)
                            Return nextState
                        End If

                        '10.06.09.LEE END ============>>
                    End If
                    ''過去履歴あるので取り消し可能-
                    'サーバーに送信判断
                    '   未送信の場合自分のもので送信済する
                    '   送信済の場合はサーバーに送信する
                    If Not UniqueInfo.uniqueCd Is Nothing Then

                        'サーバーから受信したもの
                        Dim actQty As Integer = UniqueInfo.actionQty

                        If checkInfo.AddCancelLabelData(task.ProcessText) Then
                            Dim windowSetFlg As Boolean = True
                            For i As Integer = 0 To pdaTask.WindowManager.GetRecordCount - 1
                                Dim getOrderData As COrderRecord = pdaTask.WindowManager.GetRecord(i)
                                If getOrderData.SysItemCd = sysitemCd Then
                                    'getOrderData.RemainedQty += inBoxQty
                                    getOrderData.RemainedQty += actQty
                                    getOrderData.ActionBoxQty += 1
                                    getOrderData.IsFraction = hasuFlg
                                    windowSetFlg = False
                                End If
                            Next

                            If windowSetFlg Then
                                Dim record As COrderRecord = pdaTask.WindowManager.CreateOrderRecord
                                With record
                                    .RowNum = "--"
                                    .SysItemCd = sysitemCd
                                    .ItemCd = sysitemCd
                                    .RemainedQty = actQty
                                    .ActionBoxQty = 1
                                    .IsFraction = hasuFlg
                                    .RowColor = System.Drawing.Color.GreenYellow
                                End With

                                ''uniqueメモリにも削除
                                'If checkInfo.DelUniqueCdData(uniqueCd) Then
                                '    pdaTask.Hardware.nextC()
                                'End If
                                '画面設定
                                CPersonal.AddWindowRecord(record, pdaTask.WindowManager)
                                pdaTask.WindowManager.SetRecordDelegate()
                                pdaTask.WindowManager.RefreshWindow(1)
                            End If

                            'ユニークコードリストで削除
                            'uniqueメモリにも削除
                            If checkInfo.DelUniqueCdData(uniqueCd) Then
                                'MessageBox.Show("UNIQUE_DATA_DELETE")
                                pdaTask.Hardware.nextC()
                            End If
                        Else
                            CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                            Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_022, Color.White)
                        End If
                        CFileLog.WriteLog("CCANCEL_LABEL_STATE2___OK", CFileLog.LEVEL_NORMAL, CFileLog.EnumLogType.READ)
                    Else
                        CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                       Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_022, Color.White)
                    End If

                    '終了コード
                Case EnumBarcodeType.END_LABEL_CODE
                    'サーバーに送信
                    pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_027, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
                    Dim delLabelArray As New ArrayList
                    delLabelArray = checkInfo.GetCancelLabelDataList(pdaTask.PdaNo, pdaTask.EmpCd, "END")

                    Dim filePath As String = CApplicationManager.GetRealPath(CApplicationManager.SETTING_PATH, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                    CBgControl.BgThread("CANCEL_LABEL", "csv", filePath, delLabelArray, CConst.CANCEL_UPLOAD)


                Case EnumBarcodeType.EMP_CODE
                    '交代
                    Debug.WriteLine("社員番号読みました。")

                    If Not AppManager.m_AppSetting.IsStaticReadEmpCd Then
                        '社員番号
                        '交代
                        'waitTask.Waiting(CCommon.CNS_MSG_064, pdaTask.WindowManager.GetWindowMode())
                        Dim EmpData As CCheck.EmpData = checkInfo.ChkEmpData(task.ProcessText)
                        pdaTask.WindowManager.SetEmpInfo(EmpData.empCd & ":[" & EmpData.empName & "]") '
                        pdaTask.EmpCd = EmpData.empCd
                    Else
                        CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                            Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_020, Color.White)
                    End If
                    task.ProcessText = ""
                    task.ActionCd = "CANCEL_LABEL"
                    Return DoBaseExecute(task)
                Case Else
                    '社内ラベル以外はエラー
                    CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                       Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)
                    task.ProcessText = ""
                    task.ActionCd = "CANCEL_LABEL"
                    Return DoBaseExecute(task)
            End Select

        Catch ex As Exception

        End Try

        task.ProcessText = ""
        task.ActionCd = "LABEL"
        nextState = GetNextState(task)
        Return nextState
        '注文番号存在チェッ
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CCANCELLabelState.DoExit")
        Return True
    End Function


End Class
